//
//  RNIOSExportJsToReact.m
//  xmgjAppFront
//
//  Created by 汪滔 on 2020/7/1.
//

#import <Foundation/Foundation.h>
#import "WindowModule.h"
#import <React/RCTLog.h>

@interface WindowModule()
@property (nonatomic, strong)MBProgressHUD *hud;
@end

@implementation WindowModule

RCT_EXPORT_MODULE();
//显示等待层
RCT_EXPORT_METHOD(showLoading){
  dispatch_async(dispatch_get_main_queue(), ^{
    self.hud = [MBProgressHUD cr_showLoadinWithView:[UIApplication sharedApplication].keyWindow.rootViewController.view text:@""];
  });
}
RCT_EXPORT_METHOD(dismissLoading){
  dispatch_async(dispatch_get_main_queue(), ^{
    [self.hud cr_hide];
  });
}

RCT_EXPORT_METHOD(sendRnPageCompleted){
  
}
RCTResponseSenderBlock sender;
RCT_EXPORT_METHOD(showWindow:(NSDictionary *)params callback:(RCTResponseSenderBlock) callback){
  [self showAlert:params];
  sender=callback;
}


-(id)init{
  if (self=[super init]) {
  }
  return self;
}


-(void)showAlert:(NSDictionary*)userInfo{
  NSString *title=![GJUtil GJ_isNull:userInfo[@"title"]]?@"温馨提示":userInfo[@"title"];
  NSString *content=![GJUtil GJ_isNull:userInfo[@"content"]]?@"未知错误":userInfo[@"content"];
  NSString *leftStr=![GJUtil GJ_isNull:userInfo[@"leftStr"]]?@"取消":userInfo[@"leftStr"];
  NSString *rightStr=![GJUtil GJ_isNull:userInfo[@"rightStr"]]?@"确定":userInfo[@"rightStr"];
  NSString *onlyRightBtn=![GJUtil GJ_isNull:userInfo[@"onlyRightBtn"]]?@"0":userInfo[@"onlyRightBtn"];
  dispatch_async(dispatch_get_main_queue(), ^{
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:content preferredStyle:UIAlertControllerStyleAlert];

          UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:leftStr style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
               [self showWindowCallback:@"0"];
          }];
          UIAlertAction *okAction = [UIAlertAction actionWithTitle:rightStr style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
               [self showWindowCallback:@"1"];
          }];
          UIAlertAction *centerAction = [UIAlertAction actionWithTitle:rightStr style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [self showWindowCallback:@"1"];
             }];
    if ([onlyRightBtn isEqual:@"1"]) {
       [alertController addAction:centerAction];
    } else {
      [alertController addAction:cancelAction];
      [alertController addAction:okAction];
    }

    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
     });
 
}



-(void)showWindowCallback: (NSString *)userInfoString
{
  if (sender == nil) {
    return;
  }
  sender(@[userInfoString]);
}
@end
